package com.jingan.nursinghome.regulatory.dialog.businessside;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.adapter.MicrometerTableAdapter;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.dialog.micrometertable.XuantiDialog2;
import com.jingan.nursinghome.regulatory.http.Http;
import com.jingan.nursinghome.regulatory.model.QfinfoTopic;
import com.jingan.nursinghome.regulatory.model.Topicinfo;
import com.jingan.nursinghome.regulatory.view.MyActionBar;
import com.jingan.nursinghome.regulatory.view.TriangelView;
import com.jingan.nursinghome.regulatory.view.TriangelView2;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.utils.StringUtils;

/**
 * 评分详情页
 * Created by 彭治铭 on 2018/4/1.
 */

public class MicrometerTableDetailsDialog extends BaseDialog implements View.OnClickListener {
    private static MicrometerTableDetailsDialog detailsDialog;

    static String iid;//巡检信息id

    public static MicrometerTableDetailsDialog getInstance(BaseActivity baseActivity, String id) {
        iid = id;
        if (isfinish(baseActivity, detailsDialog)) {
            detailsDialog = new MicrometerTableDetailsDialog(baseActivity);
        }
        return detailsDialog;
    }

    public MicrometerTableDetailsDialog(BaseActivity baseActivity) {
        init(baseActivity, R.layout.dialog_business_details);
    }

    MyActionBar myActionBar;
    RecyclerView recyclerView;
    Button btn_back, btn_next,btn_xuanti;

    LinearLayoutManager linearLayoutManager;
    @Override
    protected void initUI() {
        myActionBar = (MyActionBar) findViewById(R.id.MyActionBar);
        myActionBar.setLeftBimapAndTitle(null, "评分详情");
        myActionBar.setBaseListennerLeft(new BaseListenner() {
            @Override
            public void onResult(Object result) {
                dismiss();
            }
        });
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        btn_back = (Button) findViewById(R.id.btn_back);
        btn_xuanti= (Button) findViewById(R.id.btn_xuanti);
        btn_back.setOnClickListener(this);
        btn_next = (Button) findViewById(R.id.btn_next);
        btn_next.setOnClickListener(this);
        btn_xuanti.setOnClickListener(this);
    }

    @Override
    protected void listener() {
//        recyclerView.setAdapter(new MicrometerTableDetailsDialog.MyAdapter());
////        recyclerView.getAdapter().notifyDataSetChanged();
        Http.getInstance().qfinfotopiclist(getActivity(), iid, new BaseListenner<List<Topicinfo>>() {
            @Override
            public void onResult(List<Topicinfo> result) {
                models.clear();
                models.addAll(result);
                recyclerView.getAdapter().notifyDataSetChanged();
            }
        });
        recyclerView.setAdapter(new MicrometerTableAdapter2(baseActivity,models));
    }

    @Override
    protected void recycleView() {

    }
    int pageIndex;
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back://返回
                dismiss();
                break;
            case R.id.btn_next://下一项
                MicrometerTableResultDialog.getInstance(getBaseActivity(), iid);
                dismiss();
                break;
            case R.id.btn_xuanti://选题
                pageIndex=linearLayoutManager.findFirstVisibleItemPosition();
                Log.e("test","选题页数:\t"+pageIndex);
                XuantiDialog2.getInstance(baseActivity,models,pageIndex, new BaseListenner<Integer>() {
                    @Override
                    public void onResult(Integer result) {
                        //返回当前页面
                        pageIndex = result;
                        //recyclerView.smoothScrollToPosition(pageIndex);
                        //linearLayoutManager.scrollToPosition(pageIndex);
                        linearLayoutManager.scrollToPositionWithOffset(pageIndex - 1, 0);
                    }
                });
                break;
        }
    }

    private List<Topicinfo> models = new ArrayList();

//    class MyAdapter extends RecyclerView.Adapter {
//
//        class MyViewHolder extends RecyclerView.ViewHolder {
//            TextView txt_type;//得分率
//            TextView txt_rectification;//是否整改
//            TextView txt_totalscrol;//总分
//            TextView getScore;//得分
//            TextView txt_timu;//题目
//            TriangelView2 taringe;//標號
//            TextView txt_title;//分类
//
//            TextView txt_ww;//整改文字
//            public MyViewHolder(View itemView) {
//                super(itemView);
//                txt_type = itemView.findViewById(R.id.txt_type);
//                txt_rectification = itemView.findViewById(R.id.txt_rectification);
//                txt_totalscrol = itemView.findViewById(R.id.txt_totalscrol);
//                getScore = itemView.findViewById(R.id.getScore);
//                taringe = itemView.findViewById(R.id.taringe);
//                txt_timu = itemView.findViewById(R.id.txt_timu);
//                txt_ww=itemView.findViewById(R.id.txt_ww);
//                txt_title=itemView.findViewById(R.id.txt_title);
//            }
//        }
//
//
//        @Override
//        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//
//            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_business_details, parent, false);
//            return new MicrometerTableDetailsDialog.MyAdapter.MyViewHolder(view);
//        }
//
//        @Override
//        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//            if (holder instanceof MicrometerTableDetailsDialog.MyAdapter.MyViewHolder) {
//                MicrometerTableDetailsDialog.MyAdapter.MyViewHolder myViewHolder = (MicrometerTableDetailsDialog.MyAdapter.MyViewHolder) holder;
//                if (position % 2 == 0) {
//                    Animation animation_alpha = AnimationUtils.loadAnimation(getActivity(), R.anim.zebra_right_in_without_alpha);
//                    holder.itemView.startAnimation(animation_alpha);
//                } else {
//                    Animation animation_alpha = AnimationUtils.loadAnimation(getActivity(), R.anim.zebra_left_in_without_alpha);
//                    holder.itemView.startAnimation(animation_alpha);
//                }
//                QfinfoTopic qfinfoTopic = models.get(position);
//                if((qfinfoTopic.getTopic_type()!=null&&qfinfoTopic.getTopic_type().equals("2"))||qfinfoTopic.getTopic_score()==null){
//                    //违法
//                    myViewHolder.txt_totalscrol.setText("——");//总分
//                    myViewHolder.getScore.setText("——");//得分
//                    myViewHolder.txt_type.setText("——");//得分率
//
//                    //是否违法
//                    myViewHolder.txt_ww.setText("是否违法");
//                    myViewHolder.txt_rectification.setText("不违法");
//                    if(qfinfoTopic.getZhengai()!=null){
//                        if (qfinfoTopic.getZhengai().equals("0")) {
//                            myViewHolder.txt_rectification.setText("不违法");
//                        }
//                        if (qfinfoTopic.getZhengai().equals("3")) {
//                            myViewHolder.txt_rectification.setText("违法");
//                        }
//                    }
//
//                }else {
//                    //小题
//                    float tatal = Float.valueOf(qfinfoTopic.getTopic_score());//总分
//                    float score = Float.valueOf(qfinfoTopic.getPscore());//得分
//                    float prop = score / tatal * 100;//得分率
//                    myViewHolder.txt_totalscrol.setText(tatal + "分");//总分
//                    myViewHolder.getScore.setText(score + "分");//得分
//                    myViewHolder.txt_type.setText(StringUtils.getInstance().double2String(prop, 2) + "%");//得分率
//
//                    //是否整改
//                    myViewHolder.txt_ww.setText("是否整改");
//                    if (qfinfoTopic.getZhengai().equals("0")) {
//                        myViewHolder.txt_rectification.setText("无");
//                    }
//                    if (qfinfoTopic.getZhengai().equals("1")) {
//                        myViewHolder.txt_rectification.setText("现场整改");
//                    }
//                    if (qfinfoTopic.getZhengai().equals("2")) {
//                        myViewHolder.txt_rectification.setText("计划整改");
//                    }
//                }
//
//                if (qfinfoTopic.getSubjectcategory_name() != null) {
//                    myViewHolder.txt_title.setVisibility(View.VISIBLE);
//                    myViewHolder.txt_title.setText(qfinfoTopic.getSubjectcategory_name());
//                } else {
//                    myViewHolder.txt_title.setVisibility(View.GONE);
//                }
//
//                String i = (position + 1) + "";//题号
//                if (i.length() == 1) {
//                    i = "0" + i;
//                }
//                if (qfinfoTopic.getYid() != null) {
//                    myViewHolder.taringe.setIndex(qfinfoTopic.getYid());//题号
//                } else {
//                    myViewHolder.taringe.setIndex(i);//题号
//                }
//
//                //myViewHolder.taringe.setIndex(position + 1 + "");//標號
//                myViewHolder.txt_timu.setText(qfinfoTopic.getTtitle());//题目
//            }
//        }
//
//        @Override
//        public int getItemCount() {
//            return models.size();
//        }
//
//        @Override
//        public int getItemViewType(int position) {
//            return position;
//        }
//    }

}
